<%@ Page Title="" Language="C#" MasterPageFile="~/Themes/Default/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="MvcContrib.UI.Html" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index</h2>
   <table>
    <tr>
    <td colspan="2">
    
    </td>
    </tr>
    <tr><td style="width:220px;vertical-align:top">
    <div class="tree-themeroller ui-widget-content ui-corner-all" id="treeview" style="width:218px; height:420px; ">
    <ul>
        <%Html.RenderPartial("WarehouseItemControl", ViewData.Model,ViewData);%>
    </ul>
    </div>
    </td>
    <td style="vertical-align:top">
    <div class="tabcontainer">
        <div id="searchTabs">
        <ul>
            <li><a href="#basicSearch"><span><%=Html.Resource("Global","Search") %></span></a></li>
        </ul>
            <div id="basicSearch" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
            </div>
        </div>
    </div>
        <div class="tabcontainer">
        <input type="submit" value="<% =Html.Resource("Global","Search") %>" name="btnSubmit" class="button" id="btnSubmit"/>
        <input type="button" value="<% =Html.Resource("Global","CLEAR") %>" name="btnClear" class="button" id="btnClear"/>
        </div>
    <div class="tabcontainer">
        <table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
        <div id="pager" class="scroll" style="text-align:center;"></div>
    </div>
    </td>
    </tr>
    <tr>
    </tr>
    </table>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
        <%=Html.Stylesheet("~/Content/jstree/themes/themeroller/style.css")%>
    <%=Html.ScriptInclude("~/Scripts/sarissa.js") %>
    <%=Html.ScriptInclude("~/Content/jstree/jquery.tree.min.js") %>
    <%=Html.ScriptInclude("~/Content/jstree/plugins/jquery.tree.contextmenu.js")%>
    <%=Html.ScriptInclude("~/Content/jstree/plugins/jquery.tree.hotkeys.js")%>
    <%=Html.ScriptInclude("~/Content/jstree/plugins/jquery.tree.themeroller.js")%>
        <%=Html.Stylesheet("~/Content/jgGrid/ui.jqgrid.css")%>
	    <%=Html.ScriptInclude("~/Scripts/jgGrid/i18n/grid.locale-"+Html.GetjqGridCulture()+".js")%>
        <%=Html.ScriptInclude("~/Scripts/jgGrid/jquery.jqGrid.min.js")%>
        <%=Html.ScriptInclude("~/Scripts/popupHelper.js")%>
</asp:Content>

<asp:Content ID="Content4" ContentPlaceHolderID="PostScript" runat="server">
    <script type="text/javascript" language="javascript">
        $(document).ready(function() {
            jQuery("#list").jqGrid({
                url: '/Warehouse/ListData/?warehouseid=<% =(string)ViewData["rootid"] %>',
                datatype: 'json',
                mtype: 'GET',
                colNames: ['ID', '<% =Html.Resource("Warehouse","WAREHOUSECODE") %>', '<% =Html.Resource("Warehouse","WAREHOUSENAME") %>', '<% =Html.Resource("Warehouse","NODETYPE") %>', '<% =Html.Resource("Warehouse","HASBATCHNO") %>'],
                colModel: [
                  { name: 'id', index: 'id', hidden: true },
                  { name: 'warehouseCode', index: 'warehouseCode', width: 100, align: 'left' },
                  { name: 'warehouseName', index: 'warehouseName', width: 100, align: 'left' },
                  { name: 'nodeType', index: 'nodeType', width: 100, align: 'left' },
                  { name: 'hasBatchNo', index: 'hasBatchNo', width: 100, align: 'center'}],
                pager: jQuery('#pager'),
                sortname: 'warehouseCode',
                sortorder: "asc",
                viewrecords: true,
                imgpath: '/scripts/themes/coffee/images',
                caption: '',
                hidegrid: true,
                //width: 700,
                autowidth: true,
                height: 200,
                multiselect: true,
                editurl: '/Warehouse/Edit/'
            }).navGrid('#pager', {
                refresh: true,
                edit: false,
                add: false,
                del: true,
                search: false
            },
                    {}, // edit parameters
	                {}, // add parameters
	                {reloadAfterSubmit: true, url: '/Warehouse/Delete'} //delete parameters
                ).navButtonAdd('#pager', {
                    caption: "",
                    title: '<%=Html.Resource("Warehouse","EDITSELECTEDWAREHOUSE") %>',
                    buttonicon: "ui-icon-pencil",
                    onClickButton: function() {
                        var id = jQuery("#list").getGridParam('selrow');
                        if (id) {
                            var row = jQuery("#list").getRowData(id);
                            redirectToAction('Warehouse', 'Edit', row.id);
                        }
                    },
                    position: "first"
                }).navButtonAdd('#pager', {
                    caption: "",
                    title: '<%=Html.Resource("Warehouse","CREATEWAREHOUSE") %>',
                    buttonicon: "ui-icon-plus",
                    onClickButton: function() {
                        redirectToAction('Warehouse', 'Create');
                    },
                    position: "first"
                }).navButtonAdd('#pager', {
                    caption: "",
                    title: '<%=Html.Resource("Warehouse","VIEWWAREHOUSE") %>',
                    buttonicon: "ui-icon-circle-zoomin",
                    onClickButton: function() {
                        var id = jQuery("#list").getGridParam('selrow');
                        if (id) {
                            var row = jQuery("#list").getRowData(id);
                            redirectToAction('Warehouse', 'DetailView', row.id);
                        }
                    },
                    position: "first"
                });
                
                
            //initialize search buttons
            var sg = jQuery("#basicSearch").filterGrid("list",
        	    {
        	        gridModel: false,
        	        enableSearch: false,
        	        enableClear: false,
        	        autosearch: false,
        	        formtype: 'vertical',
        	        filterModel: [{
        	            label: '<% =Html.Resource("Warehouse","warehouseCode") %>',
        	            name: 'warehouseCode',
        	            stype: 'text'
        	        }, {
        	            label: '<% =Html.Resource("Warehouse","warehouseName") %>',
        	            name: 'warehouseName',
        	            stype: 'text'
        	        }
        		        ]
        	    })[0];
            jQuery("#basicSearch input").addClass("button normalinput");
            jQuery("#basicSearch td").each(function(index) {
                if (index % 2 == 0)
                    jQuery(this).css("width", "18%");
                else
                    jQuery(this).css("width", "82%");
            });

            jQuery("#btnSubmit").click(function() {
                sg.triggerSearch();
            });
            jQuery("#btnClear").click(function() {
                sg.clearSearch();
            });

            //intialize search tab
            $("#searchTabs").tabs();

            jQuery("#treeview").tree({
                rules: {
                    valid_children: ["R"]
                },
                types: {
                    "default": { draggable: false },
                    "R": { draggable: false, deletable: false, renameable: false },
                    "W": { valid_children: ["A", "L"] },
                    "A": { valid_children: ["L"] },
                    "L": { valid_children: "none", max_depth: 0, max_children: 0 }
                },
                plugins: {
                    contextmenu: {},
                    hotkeys: {}
                },
                ui: { theme_name: "classic" },
                callback: {
                    beforecreate: function() {
                        //console.log("before create");
                        return true;
                    },
                    ondblclk: function(node, treeObj) {

                        jQuery("#list").setGridParam({ url: "/Warehouse/ListData/?q=1&warehouseid=" + $(node).attr("warehouseid"), page: 1 }).trigger('reloadGrid');
                        return true;
                    },
                    oncreate: function(node, refnode, type, treeObj, rb) {
                        isCreate = true;
                        //console.log(treeObj.get_text(node));
                        //console.log("create a new node");
                    },
                    beforedelete: function(node, treeObj) {
                        
                        if (confirm("Are you sure to delete the warehouse: " + $(node).text().trim())) {
                            return true;
                        }
                        return false;
                    },
                    ondelete: function(node, treeObj) {
                        jQuery.post("/Warehouse/Delete",
                                { id: $(node).attr("warehouseid") },
                                function(data) {
                                    //console.log(data);
                                    //console.log("delete a node");
                                });
                    },
                    onrename: function(node, treeObj) {
                        if (isCreate) {
                            jQuery.post("/Warehouse/Create",
                                { departname: treeObj.get_text(node), parentid: treeObj.parent(node).attr("warehouseid") },
                                function(data) {
                                    jQuery(node).attr("warehouseid", data);
                                })
                            return;
                        }
                        jQuery.post("/Warehouse/Rename",
                                { id: jQuery(node).attr("warehouseid"), departname: treeObj.get_text(node) },
                                function(data) {
                                    //console.log(data);
                                    //console.log("rename a node");
                                });
                        isCreate = false;
                    }
                }
            });
            //expand all tree nodes
            jQuery.tree.focused().open_all();
        });
    </script>
</asp:Content>
